﻿namespace TfsExtensions.Build.Activities
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Activities;

    /// <summary>
    /// Invoke Command class
    /// </summary>
    public static class InvokeCommand
    {
        private static CodeActivityContext activityContext;

        public static void ExecuteCommand(CodeActivityContext context, string exeFileName, string arguments)
        {
              ExecuteCommand(context, false, exeFileName, arguments);
        }

        /// <summary>
        /// Executes the command.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="exeFileName">Name of the exe file.</param>
        /// <param name="arguments">The arguments.</param>
        public static void ExecuteCommand(CodeActivityContext context, bool continueOnError, string exeFileName, string arguments)
        {
            try
            {
                activityContext = context;
                using (Process process = new Process())
                {
                    var startInfo = process.StartInfo;
                    startInfo.RedirectStandardOutput = true;
                    startInfo.RedirectStandardError = true;
                    startInfo.UseShellExecute = false;
                    startInfo.FileName = exeFileName;
                    startInfo.CreateNoWindow = true;
                    startInfo.Arguments = arguments;
                    LogHelper.LogMessage(context, "{0} {1}", exeFileName , arguments);
                    process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
                    process.Start();
                    process.BeginOutputReadLine();
                    string error = process.StandardError.ReadToEnd();
                    if (!string.IsNullOrEmpty(error))
                    {
                        LogHelper.LogMessage(context, error);
                    }

                    process.WaitForExit();
                    LogHelper.LogMessage(context, "Exit Code: {0}",process.ExitCode);

                    if (!continueOnError)
                    {
                        if (process.ExitCode != 0)
                        {
                            throw new ApplicationException("Process exited with non zero exit code, Please refer logs for details");
                        }
                    }
                }
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Handles the OutputDataReceived event of the process control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Diagnostics.DataReceivedEventArgs"/> instance containing the event data.</param>
        private static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(e.Data))
                {
                    LogHelper.LogMessage(activityContext, e.Data);
                }
            }
            catch
            {
            }
        }
    }
}
